﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace Omes.Services
{
    public class DbBase
    {
        public OleDbConnection Con;

        public DbBase()
        {
            string dbfore = ConfigurationManager.AppSettings["dbfore"];
            string dbback = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["dbback"]);
            string dbPath = dbfore + dbback;
            Con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\huoguang\asman\xiangzy\omes\Omes\FashionMural.Web\App_Data\Database.mdb");
            Con.Open();
        }

        public void CloseCon()
        {
            Con.Close();
        }

        public OleDbCommand GetOleDbCommand()
        {
            OleDbCommand com = null;
            com = Con.CreateCommand();
            return com;
        }

        public DataSet GetDataSet(string cmd)
        {
            DataSet result = new DataSet();
            try
            {
                OleDbCommand com = this.GetOleDbCommand();
                com.CommandText = cmd;
                com.CommandType = CommandType.Text;
                OleDbDataAdapter da = new OleDbDataAdapter(com);
                da.Fill(result);
                this.CloseCon();
            }
            catch (Exception)
            {
                result = null;
                this.CloseCon();
            }
            return result;
        }

        public bool ExcuteDb(string cmd)
        {
            bool result = false;
            try
            {
                OleDbCommand com = this.GetOleDbCommand();
                com.CommandText = cmd;
                com.CommandType = CommandType.Text;
                int temp = com.ExecuteNonQuery();
                if (temp >= 0)
                {
                    result = true;
                }
                this.CloseCon();
            }
            catch (Exception)
            {
                result = false;
                this.CloseCon();
            }
            return result;
        }

        public int ExcuteInsertDb(string cmd)
        {
            int result = -1;
            try
            {
                OleDbCommand com = this.GetOleDbCommand();
                com.CommandText = cmd;
                com.CommandType = CommandType.Text;
                int temp = com.ExecuteNonQuery();
                result = temp;
                this.CloseCon();
            }
            catch (Exception)
            {
                result = -1;
                this.CloseCon();
            }
            return result;
        }
    }
}
